package com.gp;

import java.util.LinkedList;

import com.gp.node.INode;
import com.gp.node.operator.IFather;
import com.utils.Random;

// the crossover implementation
public class Cross {
	
	// getting two chromosomes and return their child
	public static Chrome cross(Chrome ch1, Chrome ch2){
		Chrome chrome1= (Chrome)ch1.copy();
		Chrome chrome2= (Chrome)ch2.copy();
		
		LinkedList<IFather> fathers = new LinkedList<IFather>();
		LinkedList<INode> all = new LinkedList<INode>();
		
		chrome1.fillFathers(fathers);
		chrome2.fillAll(all);
		
		if(fathers.size() <= 0){
			return ch2;
		}
		if(all.size() <= 0){
			return ch1;
		}
		IFather father = fathers.get(Random.nextInt(fathers.size()));
		INode child = all.get(Random.nextInt(all.size()));
		father.setChild(child);
		return chrome1;
	}
}
